package com.sjx.boundarycallback.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

public class JumpActivityUtils {

    public static void jump(Context context, Class<?> cls, Bundle bundle, int... flags) {
        if (context == null) return;

        Intent intent = new Intent(context, cls);

        if (flags != null && flags.length > 0) {
            for (int flag: flags) {
                intent.addFlags(flag);
            }
        }

        if (bundle != null) {
            intent.putExtras(bundle);
        }

        if (isIntentAvailable(context, intent)) {
            context.startActivity(intent);
        } else {
            Log.e("JumpUtil", "jump app is not exists");
        }


    }

    public static boolean isIntentAvailable(Context context, Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

}
